package com.sunday.scoremanager.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.widgets.swipe.SwipeMenuAdapter;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.entity.Product;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.sunday.scoremanager.base.Constants.TEST_URL;

/**
 * Created by 刘涛 on 2017/10/27.
 */

public class ProductAdapter extends SwipeMenuAdapter {


    private Context mContext;
    private List<Product> dataSet;
    private LayoutInflater layoutInflater;
    private View.OnClickListener onClickListener;

    public ProductAdapter(Context context, List<Product> datas) {
        this.mContext = context;
        this.dataSet = datas;
        this.layoutInflater = LayoutInflater.from(mContext);

    }



    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return layoutInflater.inflate(R.layout.list_product_item, parent,false);
    }

    @Override
    public RecyclerView.ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ListHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ListHolder listHolder = (ListHolder) holder;
        Product item=dataSet.get(position);
        Glide.with(mContext)
                .load(item.getImage())
                .into(listHolder.productImg);
        listHolder.productName.setText(item.getProductName());
        listHolder.priceNow.setText("￥"+item.getProductPrice());
        listHolder.priceOld.setText("￥"+item.getMarketPrice());
        listHolder.priceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        listHolder.saleNum.setText("销量"+item.getSaleCount());

        listHolder.rlProduct.setTag(item);
        listHolder.rlProduct.setOnClickListener(onClickListener);




    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class ListHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.product_img)
        ImageView productImg;
        @Bind(R.id.product_name)
        TextView productName;
        @Bind(R.id.price_now)
        TextView priceNow;
        @Bind(R.id.price_old)
        TextView priceOld;
        @Bind(R.id.sale_num)
        TextView saleNum;
        @Bind(R.id.rl_product)
        RelativeLayout rlProduct;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
